﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AutoEditUI.UserControls
{
    /// <summary>
    /// BarCodeProp.xaml 的交互逻辑
    /// </summary>
    public partial class BarCodeProp : UserControl
    {
        public enum CodeEnum {
            Barcode,
            UPCA,
            EAN13
        };

        public BarCodeProp()
        {
            InitializeComponent();
            
        }

        
        public MainWindow gmainWindow { get; private set; }
        public Image gx { get; private set; }
        public UIItem gUIItem { get; private set; }

        internal void Bind(MainWindow mainWindow, Image x)
        {
            gUIItem = (UIItem)mainWindow.GeItemByTag((string)x.Tag);
            localrelate.SetItm(mainWindow, gUIItem, x);
            TextBlockTag.Text = x.Tag.ToString();
            TextBoxW.Text = x.Width.ToString();
            TextBoxH.Text = x.Height.ToString();
            gmainWindow = mainWindow;
            gx = x;
            ItemBarcode mItemBarcode = (ItemBarcode)mainWindow.GeItemByTag((string)x.Tag);
            TextBlockTag.Text = x.Tag.ToString();
            TextBoxW.Text = x.Width.ToString();
            TextBoxH.Text = x.Height.ToString();
            TextBoxValue.Text = mItemBarcode.ItemValue;
            x.SetBinding(Image.WidthProperty, new Binding("Text") { Source = TextBoxW });
            x.SetBinding(Image.HeightProperty, new Binding("Text") { Source = TextBoxH });

            TextBoxName.Text = x.Name.ToString();
            x.SetBinding(Image.NameProperty, new Binding("Text") { Source = TextBoxName });

            X.Text = x.GetValue(Canvas.LeftProperty).ToString();
            Y.Text = x.GetValue(Canvas.TopProperty).ToString();

            Binding xxx = new Binding("Text") { Source = X };
            xxx.Mode = BindingMode.TwoWay;
            x.SetBinding(Canvas.LeftProperty, xxx);

            Binding yyy = new Binding("Text") { Source = Y };
            yyy.Mode = BindingMode.TwoWay;
            x.SetBinding(Canvas.TopProperty, yyy);

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            foreach (ItemBarcode item in gmainWindow.gUIModel.ItemBarcodes)
            {
                if (item.Tag == (string)gx.Tag) {
                    item.ItemValue = TextBoxValue.Text;
                    UpdateBarcode(TextBoxValue.Text,CodeEnum.Barcode, item.W, item.H);
                    return;
                }
            }
            MessageBox.Show("No "+ (string)gx.Tag);
        }

        internal void  UpdateBarcode( string ItemValue, CodeEnum code, double w, double h)
        {

            switch (code) {
                case CodeEnum.Barcode:
                    Code128 mBarcodeMK = new Code128();
                    gx.Source = mBarcodeMK.GetCodeImageMK(ItemValue,w, h);
                    break;
                case CodeEnum.UPCA:
                    UPC mUPCMK = new UPC();
                    gx.Source = mUPCMK.GetUPCImageMK(ItemValue, w, h);
                    break;
                case CodeEnum.EAN13:
                    EAN13 mEAN13MK = new EAN13();
                    gx.Source = mEAN13MK.GetEAN13ImageMK(ItemValue, w, h);
                    break;
            }


        }
    }
}
